package com.allen.webservice.service.impl;

import com.allen.webservice.service.Callable;
import com.allen.webservice.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {



    /**
     * @description: 这里定义了接口类型的参数并调用了相关的方法，但是具体的实现可以在调用该方法时实现
     * @param: [name, callable]
     * @return: void
     */
    @Override
    public void queryStudentInfo(String name, Callable callable) {
        log.info("开始查询学生{}信息》》》》》", name);
        Boolean aBoolean = callable.validateData(name);
        if (aBoolean) {
            String data = callable.sendData(name);
            log.info("查询到学生{}的相关信息{}:", name,data);
        }else{
            log.info("暂未查询到学生{}的相关信息", name);
        }

    }

    /**
     * @description: 测试回调接口的使用
     * @param: [args]
     * @return: void
     */
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //生成BeanDefinition对象
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(StudentServiceImpl.class).getBeanDefinition();
        //设置bean对象
        beanDefinition.setBeanClass(StudentServiceImpl.class);
        //向容器注册
        applicationContext.registerBeanDefinition("studentService",beanDefinition);
        //刷新容器
        applicationContext.refresh();
        //从容器取bean
        final StudentService bean = applicationContext.getBean(StudentService.class);
        bean.queryStudentInfo("张三", new Callable() {
            @Override
            public Boolean validateData(String name) {
                if(StringUtils.isEmpty(name)){
                    log.error("查询条件不能为空！");
                    return false;
                }
                return true;
            }

            @Override
            public String sendData(String name) {

                return "上海人,性别男,年龄18岁";
            }
        });

    }
}



